
import UIKit
import SnapKit
import SwiftEventBus


class VipCell : UICollectionViewCell{
    var product: CatProduct!
    func bindViewModel( product: CatProduct,_ index: Int) {
        self.product = product
        if index == 1{
            saleLabel.isHidden = false
        }else{
            saleLabel.isHidden = true
        }
        gradientLayer.frame = containerView.bounds
        if product.isSelect{
            priceLabel.textColor = .white
            zContainerView.layer.borderWidth = 0
            zContainerView.layer.addSublayer(gradientLayer)
        }else{
            gradientLayer.removeFromSuperlayer()
            priceLabel.textColor = UIColor("#ff4ad8")
            zContainerView.layer.borderWidth = 3
        }
        nameLabel.text = product.productTitle
        priceLabel.text = product.productPrice
        
        let attributedString = NSMutableAttributedString(string: product.originalPrice)

        attributedString.addAttribute(.strikethroughStyle, value: NSNumber(value: NSUnderlineStyle.single.rawValue), range: NSMakeRange(0, attributedString.length))

        attributedString.addAttribute(.strikethroughColor, value: UIColor.white, range: NSMakeRange(0, attributedString.length))

        originPriceLabel.attributedText = attributedString
        
    }
    
    
    var gradientLayer: CAGradientLayer = {
        
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [UIColor("#3124ad").cgColor, UIColor("#c56bd6").cgColor]
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 1, y: 1)
        gradientLayer.cornerRadius = 21
        return gradientLayer
    }()
    var saleLabel: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "dazhe")
        return imageView
    }()
    
    
    var nameLabel: UILabel = {
        let titelLabel = UILabel()
        titelLabel.font = UIFont.boldSystemFont(ofSize: 14)
        titelLabel.textColor = .white
        titelLabel.text = "学习顾问"
        titelLabel.numberOfLines = 2
        return titelLabel
    }()
    
    var priceLabel: UILabel = {
        let titelLabel = UILabel()
        titelLabel.font = UIFont.boldSystemFont(ofSize: 22)
        titelLabel.textColor = UIColor("#ff4ad8")
        return titelLabel
    }()
    
    var zContainerView: UIView = {
        return UIView()
    }()
    
    
    var containerView: UIView = {
        return UIView()
    }()
    
    
    
    var originPriceLabel: UILabel = {
        let titelLabel = UILabel()
        titelLabel.font = UIFont.boldSystemFont(ofSize: 14)
        titelLabel.textColor = .white
        titelLabel.numberOfLines = 3
        
        
        return titelLabel
    }()
    
   
    
    @objc private func handleRightBarButtonTapped(_ sender: Any) {
        
        
        SwiftEventBus.post(EVENT_NAME_DELETE_ROLE, sender: product)
        print("handleRightBarButtonTapped")
    }
    
    @objc func chatTapped(tapGestureRecognizer: UITapGestureRecognizer)
    {
        //        delegate?.didTapChat(user: user)
    }
    override init(frame: CGRect) {
        super.init(frame: .zero)
        self.addSubview(zContainerView)
        self.addSubview(containerView)
       
        zContainerView.layer.addSublayer(gradientLayer)
        containerView.addSubview(nameLabel)
        containerView.addSubview(priceLabel)
        containerView.addSubview(originPriceLabel)
        
        
        zContainerView.layer.borderColor = UIColor("#9769e7").cgColor
        zContainerView.layer.borderWidth = 3
        zContainerView.layer.cornerRadius = 12
        
        containerView.addSubview(saleLabel)
        saleLabel.snp.makeConstraints { make in
            make.centerY.equalTo(containerView.snp.top).offset(-6)
            make.centerX.equalTo(containerView)
        }
        
        
        containerView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self)
            make.top.equalTo(self).offset(24)
            make.width.height.equalTo((UIScreen.main.bounds.width - 40)/3)
        }
        zContainerView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self)
            make.top.equalTo(self).offset(24)
            make.width.height.equalTo((UIScreen.main.bounds.width - 40)/3)
        }
        

        nameLabel.snp.makeConstraints { make in
            make.top.equalTo(containerView).offset(12)
            make.centerX.equalTo(containerView)
           
        }
        
        
        
        priceLabel.snp.makeConstraints { make in
            make.center.equalTo(containerView)
            
        }
        
        
        originPriceLabel.snp.makeConstraints { make in
            make.centerX.equalTo(containerView)
            make.bottom.equalTo(containerView).offset(-12)
        }
        
        
        
       
    }
    
  
  
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

